package jstudy.gu.kit.string;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Blob;
import java.util.Collection;
import java.util.stream.Stream;

import lombok.SneakyThrows;

public final class StringKit {
	@SneakyThrows
	public static String of(Object o) {
		if (o == null) {
			return "";
		}
		if (o instanceof String || o instanceof CharSequence) {
			return o.toString();
		}
		if (o instanceof Collection<?>) {
			return of((Collection<?>) o);
		}
		if (o instanceof Stream) {
			return of((Stream<?>) o);
		}
		if (o instanceof byte[]) {
			return new String((byte[]) o, "utf-8");
		}
		if (o instanceof Blob) {
			return of((Blob) o);
		}
		return o.toString();
	}
	
	public static String parseException(Throwable e) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);
		StringBuilder sb = new StringBuilder();
		return sb.append(System.lineSeparator()).append(sw).append(System.lineSeparator()).toString();
	}


}
